package com.test.algorithm;

import org.apache.shardingsphere.api.sharding.standard.RangeShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.RangeShardingValue;

import java.util.Arrays;
import java.util.Collection;

//分库精确查询策略
public class MyRangeDSShardingAlgorithm implements RangeShardingAlgorithm<Long> {
    /**
     * select * from course where cid between 1 and 100;
     *
     * @param availableTargetNames 真实数据源集合[m1,m2]
     * @param shardingValue 逻辑表，主键，主键值
     * @return 所有数据源
     */
    @Override
    public Collection<String> doSharding(Collection<String> availableTargetNames, RangeShardingValue<Long> shardingValue) {
        //逻辑表
        String logicTableName = shardingValue.getLogicTableName();
        //主键
        String columnName = shardingValue.getColumnName();
        //范围查询主键值最大值和最小值
        Long upperVal = shardingValue.getValueRange().upperEndpoint();//100
        Long lowerVal = shardingValue.getValueRange().lowerEndpoint();//1

        //范围查询返回所有数据源
        return Arrays.asList("m1","m2");
    }
}
